import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:school_assistant_m/app/routes/app_pages.dart';
import 'package:school_assistant_m/utils/pp_template_list_view.dart';

import '../controllers/homework_controller.dart';

class HomeworkView extends GetView<HomeworkController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          // backgroundColor: Colors.red,
          title: Text(
            '作业',
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w600,
            ),
          ),
          actions: <Widget>[
            PopupMenuButton(
              itemBuilder: (BuildContext context) {
                return getPopupList();
              },
              offset: Offset(0.0, 55.0),
              elevation: 3.0,
              // color: Colors.lightBlue,
              icon: Icon(Icons.add_circle_outline, size: 25, color: Colors.black),
              onSelected: (value) async {
                print('点击的是$value');
                Get.toNamed(Routes.ADD_HOMEWORK);
              },
            ),
            SizedBox(width: 10.0),
          ],
        ),
        body: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: Stack(
            alignment: Alignment.center,
            children: <Widget>[
              controller.obx((state) => Center(
                child: PpTemplateListView(
                  title: 'homework_name',
                  title1: '学生',
                  desc1: 'runnerName',
                  code: 'courseType',
                  title2: '作业开始时间',
                  desc2: 'runStartTime',
                  title3: '作业结束时间',
                  desc3: 'runEndTime',
                  ppTitle : '结果',
                  ppDesc: 'taskResult',
                  dataForm: controller.dataForm,
                  onPressed: (Map<dynamic, dynamic> value) {  },
                  onTap: (id) {
                    print(id);
                    controller.getHomeworkById(id);
                  },
                  dropdown: () {
                    controller.getHomeworkList();
                  },
                  pullup: () { controller.getHomeworkList(); },
                ),
              ),
                onError: (err) {
                  return Text('$err');
                },
                onLoading: Container(
                  width: double.infinity,
                  height: double.infinity,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      CircularProgressIndicator(),
                      SizedBox(height: 10),
                      Text(
                        "疯狂加载中...",
                        style: TextStyle(color: Colors.blue, fontSize: 16),
                      ),
                    ],
                  ),
                ),
              )
            ],
          ),
        )
    );
  }
  List<PopupMenuEntry> getPopupList() {
    if (controller.isAdmin||controller.role.toString()=='老师') {
      return [
        PopupMenuItem(
          child: _buildPopupMenuItem(Icons.book, "布置作业"),
          value: 'add_homework',
        ),
        // PopupMenuDivider(height: 1.0),
      ];
    } else {
      return [
        PopupMenuItem(
          child: _buildPopupMenuItem(Icons.book, "完成作业"),
          value: 'finish_homework',
        ),
        // PopupMenuDivider(height: 1.0),
      ];
    }
  }

  _buildPopupMenuItem(IconData iconName, String title) {
    return Column(
      children: [
        Row(
          children: <Widget>[
            Icon(iconName, size: 22.0, color: Colors.black),
            SizedBox(width: 12.0),
            Text(title, style: TextStyle(color: Colors.black)),
          ],
        ),
      ],
    );
  }
}
